package com.wing.robot.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.robot.dao.RobotTaskDao;
import com.wing.robot.model.entity.RobotTask;
import com.wing.robot.model.entity.RobotTaskInfo;
import com.wing.robot.model.request.RobotTaskForm;
import com.wing.robot.model.response.RobotTaskVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 复合机器人任务-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class RobotTaskServiceImpl extends ServiceImpl<RobotTaskDao, RobotTask> implements RobotTaskService {
    @Autowired
    private RobotTaskInfoService robotTaskInfoService;

    @Override
    public void add(RobotTaskForm robotTaskForm) {

        //校验

        RobotTask robotTask =new RobotTask();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(RobotTask.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(robotTaskForm, robotTask, copyOptions);
        baseMapper.insert(robotTask);
        for(RobotTaskInfo robotTaskInfo:robotTaskForm.getInfoList()){
            robotTaskInfo.setTaskId(robotTask.getId());
            robotTaskInfoService.getBaseMapper().insert(robotTaskInfo);
        }

    }

    @Override
    public List<RobotTaskVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<RobotTaskVO> pageList4app(Page page, Map<String, Object> params) {
        List<RobotTaskVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public RobotTaskVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }



}
